<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <search
      :statusOptions="statusOptions"
      :btnFalse="btnFalse"
      :query="query"
      @queryData="queryData"
      :labelArr="labelArr"
      @reload="reload"
    />
    <!-- 数据列表 :data 绑定渲染的数据, border 纵向边框 -->
    <el-table :data="list" border highlight-current-row style="width: 100%">
      <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column align="center" prop="title" label="文章标题">
      </el-table-column>
      <el-table-column align="center" prop="viewCount" label="浏览量">
      </el-table-column>
      <el-table-column align="center" prop="thumhup" label="点赞数">
      </el-table-column>
      <el-table-column align="center" prop="ispublic" label="是否公开">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.ispublic === 0" type="danger">不公开</el-tag>
          <el-tag v-else>公开</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1">未审核</el-tag>
          <el-tag v-if="scope.row.status === 2" type="success">审核通过</el-tag>
          <el-tag v-if="scope.row.status === 3" type="warning"
            >审核未通过</el-tag
          >
          <el-tag v-if="scope.row.status === 0" type="danger">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updateDate" label="最后更新时间">
        <template slot-scope="scope">
          {{ getForDate(scope.row.updateDate) }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="操作" width="220">
        <template slot-scope="scope">
          <el-button size="mini" @click="openView(scope.row.id)" type="primary"
            >查看
          </el-button>
          <el-button
            size="mini"
            v-if="scope.row.status === 1"
            @click="openAudit(scope.row.id)"
            type="success"
            >审核
          </el-button>
          <el-button
            size="mini"
            v-if="scope.row.status !== 0"
            @click="handleDelete(scope.row.id)"
            type="danger"
            v-permission="'advert:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pageination
      :currentPage="page.current"
      :pageSizeInfo="page.size"
      :total="page.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 审核 查看弹窗 -->
    <audit
      :isAudit="audit.isAudit"
      :title="audit.title"
      :dialogVisible="audit.dialogVisible"
      :id="audit.id"
      :remoteForm="remoteForm"
      :searchForm="searchForm"
    />
  </div>
</template>

<script>
import { ApiGetArticleList, ApiDeleteAr } from '@/api/article'
import { dateFormat } from '@/utils/date'
import pageination from '@/components/common/pageination.vue'
import search from '@/components/common/header-search.vue'
import audit from './components/audit.vue'
// 用于下拉框
const statusOptions = [
  { value: "1", label: "未审核" },
  { value: "2", label: "审核通过" },
  { value: "3", label: "审核未通过" },
  { value: "0", label: "已删除" }
]
export default {
  name: 'Article',
  components: {
    pageination,
    search,
    audit
  },
  data () {
    return {
      list: [],
      labelArr: ['文章标题', '状态'],
      page: {
        current: 1,
        size: 20,
        total: 0
      },
      query: {},
      statusOptions,
      btnFalse: false, // 控制按钮显示隐藏
      audit: {
        dialogVisible: false,
        title: '',
        id: null,
        isAudit: true
      }
    }
  },
  methods: {
    async fetchData () {
      const { data: res } = await ApiGetArticleList(this.query, this.page.current, this.page.size)
      this.list = res.records
      this.page.total = res.total
    },
    openAudit (id) { // 审核文章
      this.audit.id = id
      this.audit.title = '审核文章'
      this.audit.isAudit = true
      this.audit.dialogVisible = true
    },
    openView (id) {// 查看文章
      this.audit.id = id
      this.audit.title = '文章详情'
      this.audit.isAudit = false
      this.audit.dialogVisible = true
    },
    remoteForm () { // 关闭弹窗
      this.audit.dialogVisible = false
    },
    searchForm () {
      this.fetchData()
    },
    async handleDelete (id) { // 删除文章
      this.$confirm('确认删除文章吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDeleteAr(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      });
    },
    queryData () { // 文章查询
      this.page.current = 1
      this.fetchData()
    },
    reload () { // 重置
      this.query = {}
      this.fetchData()
    },
    getForDate (date) { // 格式化时间日期
      return dateFormat(date)
    },
    // 当前条数
    handleSizeChange (val) {
      this.page.size = val
      this.fetchData()
    },
    // 当前页数
    handleCurrentChange (val) {
      this.page.current = val
      this.fetchData()
    },
  },
  created () {
    this.fetchData()
  },
}
</script>

<style>
</style>